package com.gitee.ice1938.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
	com.belerweb:pinyin4j:2.5.1
 * Created by zengxm on 2014/12/4.
 */
public class PinyinUtils {
	HanyuPinyinOutputFormat format = null;

	public static enum Type {
		UPPERCASE, // 全部大写
		LOWERCASE, // 全部小写
		FIRSTUPPER // 首字母大写
	}

	public PinyinUtils() {
		format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	}

	public String toPinYin(String str) {
		return toPinYin(str, "", Type.UPPERCASE);
	}

	public String toPinYin(String str, String spera) {
		return toPinYin(str, spera, Type.UPPERCASE);
	}

	public String toPinYin(String str, Type type) {
		return toPinYin(str, "", type);
	}

	/**
	 * 将str转换成拼音，如果不是汉字或者没有对应的拼音，则不作转换 如： 明天 转换成 MINGTIAN
	 * 
	 * @param str 字符串
	 * @param spera 分隔符
	 * @return str  转换结果
	 */
	public String toPinYin(String str, String spera, Type type) {
		if (str == null || str.trim().length() == 0)
			return "";
		if (type == Type.UPPERCASE)
			format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		else
			format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

		String py = "";
		String temp = "";
		String[] t;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if ((int) c <= 128)
				py += c;
			else {
				try {
					t = PinyinHelper.toHanyuPinyinStringArray(c, format);

					if (t == null)
						py += c;
					else {
						temp = t[0];
						if (type == Type.FIRSTUPPER)
							temp = t[0].toUpperCase().charAt(0) + temp.substring(1);
						py += temp + (i == str.length() - 1 ? "" : spera);
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					throw new RuntimeException(e);
				}
			}
		}
		return py.trim();
	}

	// ==================================== //
	public static void main(String[] args) {
		PinyinUtils t = new PinyinUtils();
		System.out.println(t.toPinYin("明天ABCasb"));
		System.out.println(t.toPinYin("明天ABCasb", "_"));
		System.out.println(t.toPinYin("明天ABCasb", PinyinUtils.Type.UPPERCASE));
		System.out.println(t.toPinYin("明天ABCasb", "_", PinyinUtils.Type.FIRSTUPPER));
		System.out.println(t.toPinYin("明天ABCasb", "_", PinyinUtils.Type.LOWERCASE));
		String[] arr = {"授权委托书"};
		int i = 0;
		for (; i< arr.length; i++) {
			String str =arr[i];
			//_xian_chang_jian_cha_tong_zhi_shu("现场监察通知书", Scope.custom),
			String en = "_"+t.toPinYin(str, "_", PinyinUtils.Type.LOWERCASE);
			en  = en.replace("(shen_he_)", "sp");
			en  = en.replace("(qi_ye_jie_shou_tong_zhi_)", "jstz");
			en  = en.replace("(qi_ye_ti_jiao_zi_liao_)", "tjzl");
			System.out.println(en+"(\""+str+"\", Scope.custom),");
		}
	}
}
